package com.androidcourse.shounews.Fragment;

import static android.content.ContentValues.TAG;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.androidcourse.shounews.Activity.MainActivity;
import com.androidcourse.shounews.R;

public class NewsViewFragment extends Fragment implements View.OnClickListener {

    private TextView titleView;
    private WebView webView;
    private String contentUrl;
    private String title;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle=getArguments();
        if(bundle!=null){
            contentUrl=bundle.getString("contentUrl");
            title=bundle.getString("title");
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.news_content,container,false);
        // 获取 Fragment 所在的 Activity，并设置 onBackPressed() 方法
        requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                // 在这里处理返回键事件
                if(webView.canGoBack()){
                    webView.goBack();
                }else {
                    ((MainActivity)getActivity()).backFirst();
                }
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        webView=getActivity().findViewById(R.id.news_content);
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        titleView=getActivity().findViewById(R.id.news_content_title);
        titleView.setText(title);
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl(contentUrl);
        getActivity().findViewById(R.id.back).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.back){
            if (webView.canGoBack()){
                webView.goBack();
            }else {
                ((MainActivity)getActivity()).backFirst();
            }
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    }
}
